package nl.stroep.classbuilder.project
{
	import adobe.utils.ProductManager;
	import nl.stroep.classbuilder.cb.CbClass;
	import nl.stroep.classbuilder.utils.XMLUtil;
	
	public final class Project
	{
		public var classes:Vector.<CbClass>;
		public var settings:Settings;
		
		public var id:String;
		public var name:String;
		
		public function Project(name:String, id:String = null):void 
		{ 
			this.name = name;
			
			classes = new Vector.<CbClass>()
			
			settings = new Settings();
			
			if (!id) id = int(Math.random() * 777) + "-" + int(Math.random() * 777) + "-" + int(Math.random() * 777) + "-" + int(Math.random() * 777) + "-" + int(Math.random() * 777);
			
			trace("projectID", id);
		}
		
		public function addClass(cbClass:CbClass):void
		{
			classes.push(cbClass);
		}
		
		public function toXML():XML
		{
			var xml:XML = new XML('<?xml version="1.0" encoding="UTF-8"?><root/>');
			
			xml.appendChild( <project/> );
			xml.project.appendChild( XMLUtil.getNode("name", name) );
			xml.project.appendChild( XMLUtil.getNode("id", id) );
			
			xml.project.appendChild( settings.toXML() );
			
			xml.project.appendChild( <classes/> );
			
			for (var i:int = 0; i < classes.length; ++i) 
			{
				xml.project.classes.appendChild( classes[i].toXML() );
			}
			return xml;
		}
		
		public static function parseData(xml:XML):Project
		{
			var project:Project =  new Project(xml.project.name.text(), xml.project.id.text() );
			
			project.settings = Settings.parseData( xml.project.settings );
			project.classes = CbClass.parseData( xml.project.classes );
			
			return project;
		}
	}
}